home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-06-25 | 9.3 KB | 199 lines | [TEXT/MACA] |
- ; (c) Copyright 1990 by University of Massachusetts. All rights reserved.
- ; This software was conceived, designed, and written by Dan Suthers
- ; while supported by the National Science Foundation under grant number
- ; MDR 8751362, and by a fellowship from Apple Computer, Inc., Cupertino,
- ; CA. Partial support was also received from the Office of Naval Research
- ; under a University Research Initiative Grant, contract N00014-86-K-0764.
- ; Mr. Suthers created this software under his own initiative while in an
- ; academic relationship with the University of Massachusetts. The above
- ; copyright notice was a condition placed by University lawyers on approval
- ; of distribution of this software by Apple Computer, and is not meant to
- ; imply that this software was created in an employment or "work for hire"
- ; relationship between the University and Mr. Suthers.
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; File: MAPPINGS.lisp
- ; Author: Dan Suthers
- ; Created: 16-Jun-88 09:28:29
- ; Modified: 22-Jun-90 02:06:44 (Dan Suthers)
- ; Language: Common Lisp
- ; Package: UTILS
- ;
- ; Description: Macros for treating association lists as abstract mappings.
- ;
- ; (c) Copyright 1988, by Daniel D. Suthers
- ; Department of Computer and Information Science
- ; University of Massachusetts
- ; Amherst, Massachusetts 01003
- ;
- ; This software was conceived, designed, and written by Dan Suthers
- ; while supported by the National Science Foundation under grant number
- ; MDR 8751362, and by a fellowship from Apple Computer, Inc., Cupertino,
- ; CA. Partial support was also received from the Office of Naval Research
- ; under a University Research Initiative Grant, contract N00014-86-K-0764.
- ; I wish to acknowledge the generous support of Beverly Woolf, who obtained
- ; the above grants and encouraged me to pursue my own research interests in
- ; her lab. This work would not have been possible without the resources and
- ; stimulating environment of the Computer and Information Science department.
- ;
- ; Permission to use, modify, and distribute this software is granted subject
- ; to the following restrictions and understandings:
- ; 1. The file header, including this notice, shall be retained, and may be
- ; extended to include documentation of modifications to the software.
- ; 2. This material is for nonprofit educational and research purposes only.
- ; Users are requested, but not required, to inform Mr. Suthers of any
- ; noteworthy uses of this software.
- ; 3. Mr. Suthers and the University of Massachusetts make no warrantee or
- ; representation that the operation of this software will be error free,
- ; and are under no obligation to provide any services.
- ; 4. Any user of such software agrees to indemnify and hold harmless Mr.
- ; Suthers and the University of Massachusetts from all claims arising
- ; out of the use or misuse of this software, or arising out of any
- ; accident, injury, or damage whatsoever, and from all costs, counsel
- ; fees, and liabilities incurred in or about any such claim, action, or
- ; proceeding brought thereon.
- ; 5. All materials and reports developed as a consequence of the use of
- ; this software shall duly acknowledge such use, in accordance with
- ; the usual standards of acknowledging credit in academic research.
-
- ; Status: Done and tested.
- ;
- ; Changes:
- ; 22-Mar-89 Added COMPOSE-MAPPINGS.
- ; 30-Apr-90 Fixed bug in EXTEND-MAPPING; added MERGE-MAPPINGS.
- ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;
- ; All mappings are assumed to be association lists where each item in the
- ; image set appears only once in the CAR of a cons. The CDR is the image
- ; of that item under the mapping. ADJOIN-TO-IMAGE and DELETE-FROM-IMAGE
- ; assume that images are sets represented by lists; while the other macros
- ; work with either list or atomic images. Incremental extension and re-
- ; striction of mappings is supported by EXTEND-MAPPING and RESTRICT-MAPPING.
- ; The common-lisp ACONS and PAIRLIS may also be used to construct mappings.
- ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (in-package :UTILS)
-
- (export '(
- adjoin-to-image
- compose-mappings
- delete-from-image
- domain
- extend-mapping
- image
- merge-mappings
- preimage
- range
- restrict-mapping
- ))
-
- (defmacro ADJOIN-TO-IMAGE (item key mapping &key (test '#'eq))
- "adjoin-to-image <item> <key> <mapping> &key :test [Destructive Macro]
- Given that <mapping> is setf-able access to an association list with
- lists for images: ((k1 d11 ... d1n) ... (km dm1 ... dmp)); will adjoin
- <item> to the image of <key>. Creates an entry for <key> if it is not
- already one of the ki. Test defaults #'eq. <Mapping> shouldn't have side
- effects."
- `(let ((the-key ,key)
- (the-item ,item))
- (if ,mapping
- (let ((key+image (assoc the-key ,mapping :test ,test)))
- (if key+image
- (pushnew the-item (rest key+image) :test ,test)
- (push (list the-key the-item) ,mapping)))
- (setf ,mapping (list (list the-key the-item))))
- the-item))
-
- (defmacro COMPOSE-MAPPINGS (map1 map2)
- "compose-mappings <map1> <map2> [Macro]
- Returns a new mapping (freshly consed at the top level but possibly reusing
- components) in which the images of <map1> have been replaced by their images
- in <map2>. Key and image pairs in <map1> not having images in <map2> are
- copied into the composed mapping unmodified."
- `(let ((substitution-map ,map2))
- (declare (list substitution-map))
- (mapcar #'(lambda (key1+image)
- (declare (cons key1+image))
- (let ((key2+image (assoc (cdr key1+image) substitution-map)))
- (if key2+image
- (cons (car key1+image) (cdr key2+image))
- key1+image)))
- ,map1)))
-
- (defmacro DELETE-FROM-IMAGE (item key mapping &key (test '#'eq))
- "delete-from-image <item> <key> <mapping> &key :test [Destructive Macro]
- Given that <mapping> is setf-able access to an association list with
- lists for images: ((k1 d11 ... d1n) ... (km dm1 ... dmp)); will delete
- <item> from the image of <key>. Does nothing if <key> is not found.
- Test defaults #'eq. <Mapping> shouldn't have side effects."
- `(let* ((the-key ,key)
- (the-item ,item)
- (key+image (assoc the-key ,mapping :test ,test)))
- (if key+image
- (setf (cdr key+image)
- (delete the-item (cdr key+image) :test ,test)))
- the-item))
-
- (defmacro DOMAIN (mapping)
- "domain <mapping> [Macro]
- Returns a (freshly constructed) list representing the domain of the
- mapping represented by the alist <mapping>."
- `(mapcar #'car ,mapping))
-
- (defmacro EXTEND-MAPPING (element image mapping)
- "extend-mapping <element> <image> <mapping> &key :test [Destructive Macro]
- Extends the <mapping> to include <element> and its <image>. The place
- which <mapping> evaluates to is modified. Assumes <element> is not
- already there."
- `(push (cons ,element ,image) ,mapping))
-
- (defmacro IMAGE (key mapping &key (test '#'eq))
- "image <key> <mapping> &key :test [Macro]
- Returns the image of <key> under the mapping represented by the alist
- <mapping>. Assumes the mapping is 1-1. Setf accessible. The test defaults
- to #'eq."
- `(cdr (assoc ,key ,mapping :test ,test)))
-
- (defmacro MERGE-MAPPINGS (map1 map2 &key (test '#'eq))
- "merge-mappings <map1> <map2> &key test
- Returns a freshly constructed mapping which has all keys of the two
- mappings with the union of their respective images. (For efficiency,
- give <map1> the longer list.) Test defaults #'eq."
- `(let ((new-mapping (copy-alist ,map1)))
- (declare (list new-mapping))
- (dolist (key+image2 ,map2)
- (declare (cons key+image2))
- (let ((key+image1 (assoc (car key+image2) new-mapping :test ,test)))
- (if key+image1
- (setf (cdr key+image1) (union (cdr key+image1) (cdr key+image2)))
- (push (copy-list key+image2) new-mapping))))
- new-mapping))
-
- (defmacro PREIMAGE (image mapping &key (test '#'eq))
- "preimage <image> <mapping> &key :test [Macro]
- Returns the preimage of <image> under the mapping represented by the
- alist <mapping>. Assumes the inverse of the mapping is 1-1. Setf
- accessible. The test defaults to #'eq."
- `(car (rassoc ,image ,mapping :test ,test)))
-
- (defmacro RANGE (mapping)
- "range <mapping> [Macro]
- Returns a (freshly constructed) list representing the range of the
- mapping represented by the alist <mapping>."
- `(mapcar #'cdr ,mapping))
-
- (defmacro RESTRICT-MAPPING (element mapping &key (test '#'eq))
- "restrict-mapping <element> <mapping> &key :test [Destructive Macro]
-
- Restricts the <mapping> by eliminating <element>. The place which
- <mapping> evaluates to is modified. Its evaluation should not have
- side effects. Test defaults #'EQ."
- `(setf ,mapping
- (delete ,element ,mapping :key #'car :test ,test)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (provide :MAPPINGS)
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; EOF
-